package router

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
)

func QueryParams(r *gin.Engine){
	//测试GetQuery
	r.GET("/getQuery",func(ctx *gin.Context) {
		name,isOk:=ctx.GetQuery("name")
		fmt.Printf("接收到的参数为%s,%#v\n",name,isOk)

		var age int
		query_age,isOk:=ctx.GetQuery("age")
		//判断是否得到了该值
		if isOk{
			//字符串转数字
			age,_ = strconv.Atoi(query_age)
		}
		fmt.Printf("接收到的参数为%d,%#v\n",age,isOk)

		var isStudent bool
		query_isStudent,isOk:=ctx.GetQuery("isStudent")
		if isOk{
			//字符串转布尔值
			isStudent,_ = strconv.ParseBool(query_isStudent)
		}
		fmt.Printf("接收到的参数为%v,%#v\n",isStudent,isOk)
	})

	//测试GetQueryMap
	r.GET("/getQueryMap",func(ctx *gin.Context) {
		kind,isOk :=ctx.GetQueryMap("kind")
		fmt.Printf("接收到的参数为%#v,%#v\n",kind,isOk)
	})

	//测试GetQueryArray
	r.GET("/getQueryArray",func(ctx *gin.Context) {
		scores,isOk := ctx.GetQueryArray("scores")
		fmt.Printf("接收到的参数为%#v,%#v\n",scores,isOk)
	})
}